package com.wanglianyun.redis;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wanglianyun.listener.BiliLikeListener;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Description: Redis的config处理（不处理序列化则redis值会乱码）
 * Copyright: Copyright (c) 2024
 * Create Time: 2024-07-23 15:12
 *
 * @author wanglianyun
 */
@Configuration
public class RedisConfig {

    /**
     * 因为redissonclient自己实现了连接池，如果这边redisTemplate没有使用自定义的连接池的话，
     * 那么会使用redisson的连接池，就会导致redisTemplate的配置被redissonclient的配置覆盖掉，
     * 比如redisTemplate数据库会使用所redisson那边配置的数据库
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(redisSerializer);
        redisTemplate.setHashKeySerializer(redisSerializer);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer());
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer());
        return redisTemplate;
    }

    private Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer() {
        Jackson2JsonRedisSerializer<Object> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        jsonRedisSerializer.setObjectMapper(objectMapper);
        return jsonRedisSerializer;
    }

    /**
     * 配置链接工厂,定义这个bean会导致redis订阅超时
     * @return 返回
     */
//    @Bean
//    public LettuceConnectionFactory lettuceConnectionFactory() {
//        //这里可以自定义配置或者简单地返回一个新的实例让spring boot自动填充配置
//        return new LettuceConnectionFactory();
//    }

    @Bean
    public MessageListenerAdapter biliLikeListenerAdapter() {
        return new MessageListenerAdapter(new BiliLikeListener());
    }

    //可以添加多个MessageListenerAdapter，如biliLikeListenerAdapter
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory,
                                                                       @Qualifier("biliLikeListenerAdapter") MessageListenerAdapter biliLikeListenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);
        container.addMessageListener(biliLikeListenerAdapter, new ChannelTopic("__bili_like__"));

        return container;
    }
}
